package cn.javaxc.mvcwork.util.helper;
import cn.javaxc.mvcwork.framework.database.*;
import cn.javaxc.mvcwork.modules.file.FileItem;
import cn.javaxc.mvcwork.controller.MVCConfig;
import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.control.web.*;

import java.io.*;
import java.sql.Connection;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import cn.javaxc.model.base.*;

public  class URLHelper{
  public static String addTParams(HttpServletRequest request, String tmp){
		StringBuffer sb = new StringBuffer();
		StringTokenizer tokens = new StringTokenizer(tmp, "|");
		while(tokens.hasMoreTokens()){
		 String param = tokens.nextToken();
			Object obj=RequestHelper.getRequestValue(request,param);
			if (obj!=null){
				sb.append("&" + param + "=" +obj);
			}
			
		}
		return sb.toString();
  }
  public static String getTParams(HttpServletRequest request){
	return 	(String)request.getAttribute("mvcparams");
  }	
	public static String getIcon(Object icon){
		return getIcon(icon,false,"nopic.jpg");
	}
	public static String getIcon(Object _icon,String _def){
		return getIcon(_icon,false,_def);
	}
	public static String getIcon(Object _icon,Boolean thumbnail,String _def){

		String icon=_def;
		if (_icon!=null&&!"".equals(_icon)){
			icon=_icon+"";
		}
		if (thumbnail){
			return MVCConfig.getWebContext()+"ProcessFile/FileImage?id="+icon+"&version=1&thumbnail=true";
		}
		if (CommonUtil.parseInt(icon,-1)>0){
			return MVCConfig.getWebContext()+"ProcessFile/FileImage?id="+icon+"&version=1";

		}
		return getIconBydir(icon);

	}
	public static String getElementIcon(SQLMap emt,Object _icon,Boolean thumbnail){

		String icon="nopic.jpg";
		if (_icon!=null&&!"".equals(_icon)){
			icon=_icon+"";
		}
		if (CommonUtil.parseInt(icon,-1)>0){
			try {
				 FileItem thisItem = new FileItem(CommonUtil.parseInt(icon,-1));
				 String url=MVCConfig.getWebContext()+MVCConstant.PROCESS_FILE_ACTION+"?view=true&lib="+thisItem.getLinkModuleId()+"&file=/";
				 url+=AppUtils.getDatePath(thisItem.getModified()) + thisItem.getFilename();
				if (thumbnail) {
					url += "TH";
				}
				return(url);
			 } catch (Exception e) {
				cn.javaxc.mvcwork.util.common.FrameLog.error("getElementIcon "+icon,e);
			}
		}
		return getIconBydir(icon);

	}
	public static String getIconBydir(String icon) {
		if (icon.startsWith("http:")||icon.startsWith("https:")||icon.startsWith("file:")){
			return icon;
		}else if (icon.contains(".do?")){
			return icon;
		}else if (icon.contains("\\")||icon.contains("/")){
			String root=MVCConfig.getConfig().getWebRoot();
			if (root!=null){
				if (new File(root,icon).exists()){
					return MVCConfig.getWebContext()+MVCConfig.getWebmenu().getJspUrl(icon);
				}else if (new File(root,"static/"+icon).exists()){
					return MVCConfig.getWebStatic()+MVCConfig.getWebmenu().getJspUrl(icon);
				}
			}
			return MVCConfig.getWebContext()+MVCConstant.PROCESS_FILE_ACTION+"?view=true&file=/"+icon;
		}else{
			return MVCConfig.getWebContext()+MVCConfig.getConfig().get("IconDir","static/images/icons/talent/")+MVCConfig.getWebmenu().getJspUrl(icon);
		}
	}
	public static String getTTPAction(String host,String id){
//		String action="MVCAction.do";
//		return action+"?include=/mvc.web/"+host+"/"+id+".jsp";
		String action=MVCConfig.getWebContext()+"mvc.web/"+host+"/"+id+MVCConstant.TTPAction_SUFFUX+"?";
		return action;
	}
	public static String getPageId(String value){
		if (value!=null){
			int actionidx=value.lastIndexOf("/");
//			int dotidx=value.lastIndexOf(".");
			if (actionidx>0){
				String page=value.substring(actionidx+1);
				return page;
			}
		}
		
		return null;
	}
	public static String getSelectLink(HtmlSelect list,String link,String linkKey,Object appendurl,Object select) {

		String text=list.getValueFromId(select+"");
		if (text==null||text.isEmpty()||"null".equals(text)){
			if (list.size()>0){
				text=((HtmlOption)list.get(0)).getText();
			}
		}
		StringBuffer buffer=new StringBuffer("<div class='btn-group'>");
		buffer.append("<a data-toggle='dropdown' href=# class='btn btn-white btn-xs'>"+text+"<span class='ace-icon fa fa-caret-down icon-on-right'></span></a>");
		buffer.append("<ul class='dropdown-menu'>");
		for (int i=0;i<list.size() ;i++ ){
			HtmlOption emt=(HtmlOption)list.get(i);
			buffer.append("<li");
			if ((select+"").equals(emt.getValue())){
				buffer.append(" class='active'");
			}
			buffer.append("><a href=\"" + MVCConfig.getWebmenu().getJspUrl(link));
			buffer.append("&"+linkKey+"=");
			buffer.append(emt.getValue());
			buffer.append(appendurl);
			buffer.append("\">");
			buffer.append(emt.getText());
			buffer.append("</a></li>");
		}
		buffer.append("</ul></div>");
		return buffer.toString();
	}
}
